package com.wanpinghui.supplier.object.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.object.bean.FeedBackReasonBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/14
 ***/

public class FeedBackReasonAdapter extends BaseRecyclerViewAdapter<FeedBackReasonBean.KeyValueData, FeedBackReasonAdapter.ReasonHolder> {

    public FeedBackReasonAdapter(Context context) {
        super(context);
    }

    @Override
    public ReasonHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recv_feedback_reason, null);
        return new ReasonHolder(view);
    }

    @Override
    public void onBindViewHolder(ReasonHolder holder, final int position) {
        final FeedBackReasonBean.KeyValueData data = datas.get(position);
        if (data != null) {
            holder.textViewName.setText(data.value);
            holder.imageViewCheck.setSelected(data.selected);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!data.selected) {
                        setSelected(position);
                    }
                }
            });
        }
    }

    private void setSelected(int position) {
        if (getItemCount() > position) {
            for (int i = 0; i < datas.size(); i++) {
                if (position == i) {
                    datas.get(i).selected = true;
                } else {
                    datas.get(i).selected = false;
                }
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public FeedBackReasonBean.KeyValueData getSelectData() {
        if (getItemCount() > 0) {
            for (FeedBackReasonBean.KeyValueData data : datas) {
                if (data.selected) {
                    return data;
                }
            }
        }
        return null;
    }

    class ReasonHolder extends RecyclerView.ViewHolder {
        TextView textViewName;
        ImageView imageViewCheck;

        public ReasonHolder(View itemView) {
            super(itemView);
            textViewName = (TextView) itemView.findViewById(R.id.tv_name);
            imageViewCheck = (ImageView) itemView.findViewById(R.id.iv_select);
            itemView.setLayoutParams(new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }
}
